Skip to main content

AbstractValidator<T>

Base class for object validators.

Assembly: ServiceStack.dll
View Source
Declaration
public abstract class AbstractValidator<T> : IValidator<T>, IValidator, IEnumerable<IValidationRule>, IEnumerable, IRequiresRequest, IHasTypeValidators, IServiceStackValidator

Properties

CascadeMode

Sets the cascade mode for all rules within this validator.

View Source
Declaration
public CascadeMode CascadeMode { get; set; }

TypeValidators

View Source
Declaration
public List<ITypeValidator> TypeValidators { get; }

Request

View Source
Declaration
public virtual IRequest Request { get; set; }

Gateway

View Source
Declaration
public virtual IServiceGateway Gateway { get; }

Methods

IValidator.Validate(IValidationContext)

View Source
Declaration
ValidationResult IValidator.Validate(IValidationContext context)
Returns

ServiceStack.FluentValidation.Results.ValidationResult

Parameters
TypeName
ServiceStack.FluentValidation.IValidationContextcontext

IValidator.ValidateAsync(IValidationContext, CancellationToken)

View Source
Declaration
Task<ValidationResult> IValidator.ValidateAsync(IValidationContext context, CancellationToken cancellation)
Returns

System.Threading.Tasks.Task<ServiceStack.FluentValidation.Results.ValidationResult>

Parameters
TypeName
ServiceStack.FluentValidation.IValidationContextcontext
System.Threading.CancellationTokencancellation

Validate(T)

Validates the specified instance

View Source
Declaration
public ValidationResult Validate(T instance)
Returns

ServiceStack.FluentValidation.Results.ValidationResult: A ValidationResult object containing any validation failures

Parameters
TypeNameDescription
<T>instanceThe object to validate

|

ValidateAsync(T, CancellationToken)

Validates the specified instance asynchronously

View Source
Declaration
public Task<ValidationResult> ValidateAsync(T instance, CancellationToken cancellation = default(CancellationToken))
Returns

System.Threading.Tasks.Task<ServiceStack.FluentValidation.Results.ValidationResult>: A ValidationResult object containing any validation failures

Parameters
TypeNameDescription
<T>instanceThe object to validate

| | System.Threading.CancellationToken | cancellation | Cancellation token

|

Validate(ValidationContext<T>)

Validates the specified instance.

View Source
Declaration
public virtual ValidationResult Validate(ValidationContext<T> context)
Returns

ServiceStack.FluentValidation.Results.ValidationResult: A ValidationResult object containing any validation failures.

Parameters
TypeNameDescription
ServiceStack.FluentValidation.ValidationContext<T>contextValidation Context

|

ValidateAsync(ValidationContext<T>, CancellationToken)

Validates the specified instance asynchronously.

View Source
Declaration
public virtual async Task<ValidationResult> ValidateAsync(ValidationContext<T> context, CancellationToken cancellation = default(CancellationToken))
Returns

System.Threading.Tasks.Task<ServiceStack.FluentValidation.Results.ValidationResult>: A ValidationResult object containing any validation failures.

Parameters
TypeNameDescription
ServiceStack.FluentValidation.ValidationContext<T>contextValidation Context

| | System.Threading.CancellationToken | cancellation | Cancellation token

|

AddRule(IValidationRule)

Adds a rule to the current validator.

View Source
Declaration
protected void AddRule(IValidationRule rule)
Parameters
TypeName
ServiceStack.FluentValidation.IValidationRulerule

CreateDescriptor()

Creates a ServiceStack.FluentValidation.IValidatorDescriptor that can be used to obtain metadata about the current validator.

View Source
Declaration
public virtual IValidatorDescriptor CreateDescriptor()
Returns

ServiceStack.FluentValidation.IValidatorDescriptor

IValidator.CanValidateInstancesOfType(Type)

View Source
Declaration
bool IValidator.CanValidateInstancesOfType(Type type)
Returns

System.Boolean

Parameters
TypeName
System.Typetype

RuleFor<TProperty>(Expression<Func<T, TProperty>>)

Defines a validation rule for a specify property.

View Source
Declaration
public IRuleBuilderInitial<T, TProperty> RuleFor<TProperty>(Expression<Func<T, TProperty>> expression)
Returns

ServiceStack.FluentValidation.IRuleBuilderInitial<<T>,<TProperty>>: an IRuleBuilder instance on which validators can be defined

Parameters
TypeNameDescription
System.Linq.Expressions.Expression<System.Func<<T>,<TProperty>>>expressionThe expression representing the property to validate

|

Type Parameters
NameDescription
TPropertyThe type of property being validated

Transform<TProperty, TTransformed>(Expression<Func<T, TProperty>>, Func<TProperty, TTransformed>)

Defines a validation rule for a specify property and transform it to a different type.

View Source
Declaration
public IRuleBuilderInitial<T, TTransformed> Transform<TProperty, TTransformed>(Expression<Func<T, TProperty>> from, Func<TProperty, TTransformed> to)
Returns

ServiceStack.FluentValidation.IRuleBuilderInitial<<T>,<TTransformed>>: an IRuleBuilder instance on which validators can be defined

Parameters
TypeNameDescription
System.Linq.Expressions.Expression<System.Func<<T>,<TProperty>>>fromThe expression representing the property to transform

| | System.Func<<TProperty>,<TTransformed>> | to | Function to transform the property value into a different type

|

Type Parameters
NameDescription
TPropertyThe type of property being validated
TTransformedThe type after the transformer has been applied

Transform<TProperty, TTransformed>(Expression<Func<T, TProperty>>, Func<T, TProperty, TTransformed>)

Defines a validation rule for a specify property and transform it to a different type.

View Source
Declaration
public IRuleBuilderInitial<T, TTransformed> Transform<TProperty, TTransformed>(Expression<Func<T, TProperty>> from, Func<T, TProperty, TTransformed> to)
Returns

ServiceStack.FluentValidation.IRuleBuilderInitial<<T>,<TTransformed>>: an IRuleBuilder instance on which validators can be defined

Parameters
TypeNameDescription
System.Linq.Expressions.Expression<System.Func<<T>,<TProperty>>>fromThe expression representing the property to transform

| | System.Func<<T>,<TProperty>,<TTransformed>> | to | Function to transform the property value into a different type

|

Type Parameters
NameDescription
TPropertyThe type of property being validated
TTransformedThe type after the transformer has been applied

RuleForEach<TElement>(Expression<Func<T, IEnumerable<TElement>>>)

Invokes a rule for each item in the collection.

View Source
Declaration
public IRuleBuilderInitialCollection<T, TElement> RuleForEach<TElement>(Expression<Func<T, IEnumerable<TElement>>> expression)
Returns

ServiceStack.FluentValidation.IRuleBuilderInitialCollection<<T>,<TElement>>: An IRuleBuilder instance on which validators can be defined

Parameters
TypeNameDescription
System.Linq.Expressions.Expression<System.Func<<T>,System.Collections.Generic.IEnumerable<<TElement>>>>expressionExpression representing the collection to validate

|

Type Parameters
NameDescription
TElementType of property

TransformForEach<TElement, TTransformed>(Expression<Func<T, IEnumerable<TElement>>>, Func<TElement, TTransformed>)

Invokes a rule for each item in the collection, transforming the element from one type to another.

View Source
Declaration
public IRuleBuilderInitialCollection<T, TTransformed> TransformForEach<TElement, TTransformed>(Expression<Func<T, IEnumerable<TElement>>> expression, Func<TElement, TTransformed> to)
Returns

ServiceStack.FluentValidation.IRuleBuilderInitialCollection<<T>,<TTransformed>>: An IRuleBuilder instance on which validators can be defined

Parameters
TypeNameDescription
System.Linq.Expressions.Expression<System.Func<<T>,System.Collections.Generic.IEnumerable<<TElement>>>>expressionExpression representing the collection to validate

| | System.Func<<TElement>,<TTransformed>> | to | Function to transform the collection element into a different type

|

Type Parameters
NameDescription
TElementType of property
TTransformedThe type after the transformer has been applied

TransformForEach<TElement, TTransformed>(Expression<Func<T, IEnumerable<TElement>>>, Func<T, TElement, TTransformed>)

Invokes a rule for each item in the collection, transforming the element from one type to another.

View Source
Declaration
public IRuleBuilderInitialCollection<T, TTransformed> TransformForEach<TElement, TTransformed>(Expression<Func<T, IEnumerable<TElement>>> expression, Func<T, TElement, TTransformed> to)
Returns

ServiceStack.FluentValidation.IRuleBuilderInitialCollection<<T>,<TTransformed>>: An IRuleBuilder instance on which validators can be defined

Parameters
TypeNameDescription
System.Linq.Expressions.Expression<System.Func<<T>,System.Collections.Generic.IEnumerable<<TElement>>>>expressionExpression representing the collection to validate

| | System.Func<<T>,<TElement>,<TTransformed>> | to | Function to transform the collection element into a different type

|

Type Parameters
NameDescription
TElementType of property
TTransformedThe type after the transformer has been applied

RuleSet(String, Action)

Defines a RuleSet that can be used to group together several validators.

View Source
Declaration
public void RuleSet(string ruleSetName, Action action)
Parameters
TypeNameDescription
System.StringruleSetNameThe name of the ruleset.

| | System.Action | action | Action that encapsulates the rules in the ruleset.

|

When(Func<T, Boolean>, Action)

Defines a condition that applies to several rules

View Source
Declaration
public IConditionBuilder When(Func<T, bool> predicate, Action action)
Returns

ServiceStack.FluentValidation.IConditionBuilder

Parameters
TypeNameDescription
System.Func<<T>,System.Boolean>predicateThe condition that should apply to multiple rules

| | System.Action | action | Action that encapsulates the rules.

|

When(Func<T, ValidationContext<T>, Boolean>, Action)

Defines a condition that applies to several rules

View Source
Declaration
public IConditionBuilder When(Func<T, ValidationContext<T>, bool> predicate, Action action)
Returns

ServiceStack.FluentValidation.IConditionBuilder

Parameters
TypeNameDescription
System.Func<<T>,ServiceStack.FluentValidation.ValidationContext<<T>>,System.Boolean>predicateThe condition that should apply to multiple rules

| | System.Action | action | Action that encapsulates the rules.

|

Unless(Func<T, Boolean>, Action)

Defines an inverse condition that applies to several rules

View Source
Declaration
public IConditionBuilder Unless(Func<T, bool> predicate, Action action)
Returns

ServiceStack.FluentValidation.IConditionBuilder

Parameters
TypeNameDescription
System.Func<<T>,System.Boolean>predicateThe condition that should be applied to multiple rules

| | System.Action | action | Action that encapsulates the rules

|

Unless(Func<T, ValidationContext<T>, Boolean>, Action)

Defines an inverse condition that applies to several rules

View Source
Declaration
public IConditionBuilder Unless(Func<T, ValidationContext<T>, bool> predicate, Action action)
Returns

ServiceStack.FluentValidation.IConditionBuilder

Parameters
TypeNameDescription
System.Func<<T>,ServiceStack.FluentValidation.ValidationContext<<T>>,System.Boolean>predicateThe condition that should be applied to multiple rules

| | System.Action | action | Action that encapsulates the rules

|

WhenAsync(Func<T, CancellationToken, Task<Boolean>>, Action)

Defines an asynchronous condition that applies to several rules

View Source
Declaration
public IConditionBuilder WhenAsync(Func<T, CancellationToken, Task<bool>> predicate, Action action)
Returns

ServiceStack.FluentValidation.IConditionBuilder

Parameters
TypeNameDescription
System.Func<<T>,System.Threading.CancellationToken,System.Threading.Tasks.Task<System.Boolean>>predicateThe asynchronous condition that should apply to multiple rules

| | System.Action | action | Action that encapsulates the rules.

|

WhenAsync(Func<T, ValidationContext<T>, CancellationToken, Task<Boolean>>, Action)

Defines an asynchronous condition that applies to several rules

View Source
Declaration
public IConditionBuilder WhenAsync(Func<T, ValidationContext<T>, CancellationToken, Task<bool>> predicate, Action action)
Returns

ServiceStack.FluentValidation.IConditionBuilder

Parameters
TypeNameDescription
System.Func<<T>,ServiceStack.FluentValidation.ValidationContext<<T>>,System.Threading.CancellationToken,System.Threading.Tasks.Task<System.Boolean>>predicateThe asynchronous condition that should apply to multiple rules

| | System.Action | action | Action that encapsulates the rules.

|

UnlessAsync(Func<T, CancellationToken, Task<Boolean>>, Action)

Defines an inverse asynchronous condition that applies to several rules

View Source
Declaration
public IConditionBuilder UnlessAsync(Func<T, CancellationToken, Task<bool>> predicate, Action action)
Returns

ServiceStack.FluentValidation.IConditionBuilder

Parameters
TypeNameDescription
System.Func<<T>,System.Threading.CancellationToken,System.Threading.Tasks.Task<System.Boolean>>predicateThe asynchronous condition that should be applied to multiple rules

| | System.Action | action | Action that encapsulates the rules

|

UnlessAsync(Func<T, ValidationContext<T>, CancellationToken, Task<Boolean>>, Action)

Defines an inverse asynchronous condition that applies to several rules

View Source
Declaration
public IConditionBuilder UnlessAsync(Func<T, ValidationContext<T>, CancellationToken, Task<bool>> predicate, Action action)
Returns

ServiceStack.FluentValidation.IConditionBuilder

Parameters
TypeNameDescription
System.Func<<T>,ServiceStack.FluentValidation.ValidationContext<<T>>,System.Threading.CancellationToken,System.Threading.Tasks.Task<System.Boolean>>predicateThe asynchronous condition that should be applied to multiple rules

| | System.Action | action | Action that encapsulates the rules

|

Include(IValidator<T>)

Includes the rules from the specified validator

View Source
Declaration
public void Include(IValidator<T> rulesToInclude)
Parameters
TypeName
ServiceStack.FluentValidation.IValidator<T>rulesToInclude

Include<TValidator>(Func<T, TValidator>)

Includes the rules from the specified validator

View Source
Declaration
public void Include<TValidator>(Func<T, TValidator> rulesToInclude)
where TValidator : IValidator<T>
Parameters
TypeName
System.Func<<T>,<TValidator>>rulesToInclude
Type Parameters
  • TValidator

GetEnumerator()

Returns an enumerator that iterates through the collection of validation rules.

View Source
Declaration
public IEnumerator<IValidationRule> GetEnumerator()
Returns

System.Collections.Generic.IEnumerator<ServiceStack.FluentValidation.IValidationRule>: A System.Collections.Generic.IEnumerator%601 that can be used to iterate through the collection.

IEnumerable.GetEnumerator()

View Source
Declaration
IEnumerator IEnumerable.GetEnumerator()
Returns

System.Collections.IEnumerator

EnsureInstanceNotNull(Object)

Throws an exception if the instance being validated is null.

View Source
Declaration
protected virtual void EnsureInstanceNotNull(object instanceToValidate)
Parameters
TypeName
System.ObjectinstanceToValidate

PreValidate(ValidationContext<T>, ValidationResult)

Determines if validation should occur and provides a means to modify the context and ValidationResult prior to execution. If this method returns false, then the ValidationResult is immediately returned from Validate/ValidateAsync.

View Source
Declaration
protected virtual bool PreValidate(ValidationContext<T> context, ValidationResult result)
Returns

System.Boolean

Parameters
TypeName
ServiceStack.FluentValidation.ValidationContext<T>context
ServiceStack.FluentValidation.Results.ValidationResultresult

RaiseValidationException(ValidationContext<T>, ValidationResult)

Throws a ValidationException. This method will only be called if the validator has been configured to throw exceptions if validation fails. The default behaviour is not to throw an exception.

View Source
Declaration
protected virtual void RaiseValidationException(ValidationContext<T> context, ValidationResult result)
Parameters
TypeName
ServiceStack.FluentValidation.ValidationContext<T>context
ServiceStack.FluentValidation.Results.ValidationResultresult

RuleSet(ApplyTo, Action)

Defines a RuleSet that can be used to provide specific validation rules for specific HTTP methods (GET, POST...)

View Source
Declaration
public void RuleSet(ApplyTo appliesTo, Action action)
Parameters
TypeNameDescription
ServiceStack.ApplyToappliesToThe HTTP methods where this rule set should be used.

| | System.Action | action | Action that encapsulates the rules in the ruleset.

|

RemovePropertyRules(Func<PropertyRule, Boolean>)

View Source
Declaration
public void RemovePropertyRules(Func<PropertyRule, bool> where)
Parameters
TypeName
System.Func<ServiceStack.FluentValidation.Internal.PropertyRule,System.Boolean>where

Implements